package com.woniuxy.microstockserver.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.slsupermarket.common.res.ResponseMsg;
import com.woniuxy.microstockserver.bean.*;
import com.woniuxy.microstockserver.service.IEnterStockGoodsDetailService;
import com.woniuxy.microstockserver.service.IEnterStockService;
import com.woniuxy.microstockserver.service.IStockService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 入库模块Controller
 */
@RestController
@RequestMapping(value = "/enterStock")
public class EnterStockController {

    @Resource
    private IEnterStockService iEnterStockServiceImpl;

    @Resource
    private IEnterStockGoodsDetailService iEnterStockGoodsDetailServiceImpl;

    @Resource
    private IStockService iStockServiceImpl;
    /**
     * @ 分页、分类查询所有的入库单：已测试成功
     * @param pageRequestBean
     * @param enterStockBean
     * @return
     */
    @RequestMapping(value = "/findAllEnterBean",produces = {"application/json;charset=utf-8"})
    public PageReturnBean<EnterStockBean> findAllEnterBean(PageRequestBean pageRequestBean, EnterStockBean enterStockBean){
        PageInfo<EnterStockBean> allEnterStockBean = iEnterStockServiceImpl.findAllEnterStockBean(pageRequestBean, enterStockBean);
        return new PageReturnBean<EnterStockBean>(allEnterStockBean.getTotal(),allEnterStockBean.getList());
    }

    /**
     * 新增入库单：测试成功
     * @param enterStockBean
     * @param
     * @return
     */
    @PostMapping(value = "/saveEnterStockBean",produces = {"application/json;charset=utf-8"})
    public ResponseMsg saveEnterStockBean(@RequestBody EnterStockBean enterStockBean){
        Long esId = iEnterStockServiceImpl.saveEnterStockBean(enterStockBean);
        List<EnterStockGoodsDetailBean> goodsBeans = enterStockBean.getGoodsBeans();
        int result = iEnterStockGoodsDetailServiceImpl.saveEnterStockGoodsDetailMapper(esId, goodsBeans);
        if(result>0){
            return new ResponseMsg().builder().code(200).msg("操作成功").build();
        }
        return new ResponseMsg().builder().code(10000).msg("操作失败").build();
    }

    /**
     * 查看入库单详情：测试成功
     * @param id
     * @return
     */
    @RequestMapping(value = "/findEnterStockAndGoodsDetail",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findEnterStockAndGoodsDetail(Long id){
        EnterStockBean enterStock = iEnterStockServiceImpl.findEnterStockAndGoodsDetail(id);
        List<EnterStockGoodsDetailBean> enterStockGoodsDetail = iEnterStockGoodsDetailServiceImpl.findEnterStockAndGoodsDetail(id);
        enterStock.setGoodsBeans(enterStockGoodsDetail);
        if(enterStock != null){
            return new ResponseMsg().builder().code(200).msg("操作成功").data(enterStock).build();
        }
        return new ResponseMsg().builder().code(10000).msg("操作失败").build();
    }

    /**
     * 修改入库单：测试成功
     * @return
     */
    @PostMapping(value = "/upadteEnterStockBean",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg upadteEnterStockBean(@RequestBody EnterStockBean enterStockBean){
        Long aLong = iEnterStockServiceImpl.upadteEnterStockBean(enterStockBean);
        iEnterStockGoodsDetailServiceImpl.deleteByEsId(enterStockBean.getId());
        int res = iEnterStockGoodsDetailServiceImpl.saveEnterStockGoodsDetailMapper(enterStockBean.getId(), enterStockBean.getGoodsBeans());
        if(res>0){
            return new ResponseMsg().builder().code(200).msg("操作成功").build();
        }
        return new ResponseMsg().builder().code(10000).msg("操作失败").build();
    }

    @PostMapping(value = "/auditEnterStock",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg auditEnterStock(@RequestBody EnterStockBean enterStockBean) {
        //判断审核类型：0-待审核，1-审核成功，2-审核失败
        if(enterStockBean.getEsAuditStatus()==1){
            //修改审核状态
            iEnterStockServiceImpl.upadteEnterStockBean(enterStockBean);
            //入库
            /*StockBean stockBean = new StockBean();
            stockBean.setGoodsId();
            iStockServiceImpl.findByGoodsId()*/
        }else if(enterStockBean.getEsAuditStatus()==2){

        }

        return new ResponseMsg().builder().code(10000).msg("操作失败").build();
    }




}
